'use strict';

const BaseController = require('./base');
const { sign, verify } = require('jsonwebtoken');
const WXBizDataCrypt = require('../middleware/WXBizDataCrypt');
const bcrypt = require('bcryptjs');
/**
 * @Controller 用户模块
 */
class UserController extends BaseController {
  /**
   * @Router post /api/user/register
   * @Request body registerUserRequest
   * @Summary 用户注册
   */
  async create() {
    const { ctx, app, service } = this;
    const body = ctx.request.body;
    body.userId = Date.now();
    // 获取Redis中的邮件验证码
    const YZM_CODE = await app.redis.get('YZM_CODE');
    if (!YZM_CODE) {
      this.Error({ message: '验证码已过期，请重新发送' });
      return;
    }
    if (YZM_CODE !== body.authCode) {
      this.Error({ message: '验证码错误' });
      return;
    }
    delete body.authCode;
    const result = await service.user.create(body);
    if (result) {
      this.Success({ message: '新增成功' });
    } else {
      this.Error({ message: '新增失败' });
    }
  }

  /**
   * @Router post /api/user/login
   * @Request body loginUserRequest
   * @Summary 登录
   */
  async login() {
    const { ctx, service } = this;
    const params = ctx.request.body;
    const data = await service.user.login(params);
    if (!data) {
      this.Error({ message: '登录失败,该用户不存在！' });
    } else {
      if (params.password === data.password) {
        let token = sign(data.dataValues, this.config.jwtSecret, {
          expiresIn: '1h',
        });
        this.Success({ token, message: '登录成功' });
      } else {
        this.Error({ message: '登录失败,请输入正确的密码！' });
      }
    }
  }

  /**
   * @Router post /api/user/query
   * @Request body queryUserRequest
   * @Summary 找回密码 查询邮箱是否存在
   */
  async query() {
    const { ctx, service } = this;
    let payload = ctx.request.body;
    const data = await service.user.info(payload);
    if (data) {
      this.Success({ message: '获取成功' });
    } else {
      this.Error({ message: '获取失败,用户不存在' });
    }
  }

  /**
   * @Router post /api/user/pwd
   * @Request body pwdUserRequest
   * @Summary 找回密码
   */
  async pwd() {
    const { ctx, app, service } = this;
    const { email, password, authCode } = ctx.request.body;
    // 获取Redis中的邮件验证码
    const YZM_CODE = await app.redis.get('YZM_CODE');
    if (!YZM_CODE) {
      this.Error({ message: '邮箱验证码已过期' });
      return;
    }
    if (authCode !== YZM_CODE) {
      this.Error({ message: '邮箱验证码错误，请重试' });
      return;
    }
    const data = await service.user.pwd({ email, password });
    if (data) {
      this.Success({ message: '修改成功' });
    } else {
      this.Error({ message: '修改密码错误，请重试' });
    }
  }

  /**
   * @Router post /api/user/page
   * @Request body pageUserRequest
   * @Summary 用户分页
   */
  async page() {
    const { ctx, service } = this;
    let params = ctx.request.body;
    const data = await service.user.page(params);
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }

  /**
   * @Router post /api/user/info
   * @Request body infoUserRequest
   * @Summary 获取用户详情信息
   */
  async info() {
    const { ctx, service } = this;
    let payload = ctx.request.body;
    const data = await service.user.info(payload);
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败,用户不存在' });
    }
  }

  /**
   * @Router post /api/user/edit
   * @Request body editUserRequest
   * @Summary 修改用户信息
   */
  async edit() {
    const { ctx, service } = this;
    let body = ctx.request.body;
    const data = await service.user.edit(body);
    if (data) {
      this.Success({ message: '修改成功' });
    } else {
      this.Error({ message: '修改失败' });
    }
  }

    /**
   * @Router post /api/user/del
   * @Request body delUserRequest
   * @Summary 删除用户信息
   */
  async del() {
    const { ctx, service } = this;
    let { id } = ctx.request.body;
    const data = await service.user.del(id);
    if (data) {
      this.Success({ message: '删除成功' });
    } else {
      this.Error({ message: '删除失败' });
    }
  }

  // 退出
  async logout() {
    const { ctx, service, app } = this;
    this.Success({  message: '退出成功' });
  }

  // Token解密
  async decode() {
    const { ctx } = this;
    const { token } = ctx.request.body;
    let data = verify(token, this.config.jwtSecret);
    delete data.password;
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }
}

module.exports = UserController;
